package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponExample;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.MarketCouponUserExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: CouponServiceImpl
 * @Description: TODO
 * @Author: jenyutao@gmail.com
 * @date: 2023/5/30 16:19
 * @Version: V1.0
 */
public class CouponServiceImpl implements CouponService {

    @Override
    public CommonData<MarketCoupon> querySelective(String name, Short type, Short status, Integer page, Integer limit, String sort, String order) {

        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();

        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        MarketCouponMapper couponMapper = sqlSession.getMapper(MarketCouponMapper.class);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(example);

        TransactionUtil.commit();
        return CommonData.data(marketCoupons);

    }

    @Override
    public BaseRespVo create(MarketCoupon marketCoupon) {

        TransactionUtil.startTransaction();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date();

        TransactionUtil.startTransaction();
        marketCoupon.setAddTime(date);
        marketCoupon.setUpdateTime(date);
        marketCoupon.setDeleted(false);

        if (marketCoupon.getType().equals(2)) {
            String code = this.generateCode(8);
            marketCoupon.setCode(code);
        }

        MarketCouponMapper mapper = TransactionUtil.getMapper(MarketCouponMapper.class);
        marketCoupon.setId(null);
        int insert = mapper.insert(marketCoupon);

        TransactionUtil.commit();
        return BaseRespVo.ok(marketCoupon);

    }

    //生成随机字符串
    private String generateCode(int length) {
        String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            char randomChar = CHARACTERS.charAt(randomIndex);
            sb.append(randomChar);
        }
        return sb.toString();
    }

    @Override
    public BaseRespVo update(MarketCoupon marketCoupon) {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();

        Date date = new Date();
        marketCoupon.setUpdateTime(date);

        TransactionUtil.startTransaction();

        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        int i = mapper.updateByPrimaryKey(marketCoupon);

        TransactionUtil.commit();
        return BaseRespVo.ok(marketCoupon);

    }


    @Override
    public BaseRespVo listuser(Integer page, Integer limit, Integer couponId, Integer userId, Short status, String sort, String order) {
        TransactionUtil.startTransaction();

        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();

        if(couponId!=null) {
            criteria.andIdEqualTo(couponId);
        }

        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status!=null){
            criteria.andStatusEqualTo(status);
        }

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page,limit);

        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketCouponUserMapper mapper = sqlSession.getMapper(MarketCouponUserMapper.class);
        List<MarketCouponUser> marketCouponUsers = mapper.selectByExample(example);
        sqlSession.commit();
        sqlSession.close();
        return BaseRespVo.ok(CommonData.data(marketCouponUsers));
    }

    @Override
    public BaseRespVo delete(MarketCoupon marketCoupon) {

        TransactionUtil.startTransaction();
        MarketCouponMapper mapper = TransactionUtil.getMapper(MarketCouponMapper.class);

        marketCoupon.setDeleted(true);
        mapper.updateByPrimaryKey(marketCoupon);

        TransactionUtil.commit();

        return  BaseRespVo.ok(marketCoupon);
    }

    @Override
    public BaseRespVo read(Integer id) {

        TransactionUtil.startTransaction();
        MarketCouponMapper mapper = TransactionUtil.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = mapper.selectByPrimaryKey(id);

        TransactionUtil.commit();
        return BaseRespVo.ok(marketCoupon);

    }
}
